/**
 *  TinyPasswordManager
 *  Copyright (C) 2008 Gross David <gdavid.devel@gmail.com>
 *                     Vinet Guillaume <vinet.guillaume@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// XXX
#define QT_GUI 

#ifdef QT_GUI
#include <QApplication>
#include <QCleanlooksStyle>
#include <QTranslator>
#else

#include "tinypasswordmanager.h"
#include "sha256.h"

#include <string>
#include <sstream>
#include <iostream>

extern "C" {
    #include <unistd.h>
    #include <termios.h>
}
#endif

#ifdef QT_GUI
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    MainWindow w;

    w.setStyle(new QCleanlooksStyle());
    w.show();
    return app.exec();
}
#else
int main(int, char **) 
{
    TinyPasswordManager *tpm = TinyPasswordManager::getInstance();

    try {
        tpm->openDatabase("pm.db");
    }
    catch(ExFileNotFound &) {
    }
    catch(ExMemoryAllocationFailed &ex) {
        fprintf(stderr, "%s\n", ex.what());
        return 1;
    }
    catch(ExFileErrorReading &ex) {
        fprintf(stderr, "%s\n", ex.what());
        return 1;
    }

    struct termios term_settings;
    tcgetattr(0, &term_settings);
    term_settings.c_lflag &= ~ ECHO;
    tcsetattr(0, TCSANOW, &term_settings);
    
    std::string str;
    std::cout << "Private key : ";
    std::getline(std::cin, str);
    std::cout << std::endl;
    
    unsigned char digestKey[32]; memset(digestKey, 0, 32);
    SHA256::getInstance()->digest(str.c_str(), digestKey);

    term_settings.c_lflag |= ECHO;
    tcsetattr(0, TCSANOW, &term_settings);
    
    tpm->setPrivateKey(digestKey);

    try {
        tpm->getIdentifiers();
    }
    catch(ExMemoryAllocationFailed &ex) {
        fprintf(stderr, "%s\n", ex.what());
        return 1;
    }
    catch(ExBadFileFormat &ex) {
        fprintf(stderr, "%s\n", ex.what());
        return 1;
    }
    catch(ExFileErrorWriting &ex) {
        fprintf(stderr, "%s\n", ex.what());
        return 1;
    }

    std::ostringstream oss;
    str = "";

    do {
        std::getline(std::cin, str);

        if(str.find("show") == 0)
            for(unsigned int i = 0; i < tpm->size(); ++i) {
                oss << i << " - " << tpm->at(i).getLogin() << " : " << tpm->at(i).getDescription();
                std::cout << oss.str() << "\n";
                oss.str("");
                oss.clear();
            }

        else if(str.find("search") == 0) {
            str = str.substr(7);
            
            for(unsigned int i = 0; i < tpm->size(); ++i)
                if(tpm->at(i).getDescription().find(str) != std::string::npos) {
                    oss << i << " - " << tpm->at(i).getLogin() << " : " << tpm->at(i).getDescription();
                    std::cout << oss.str() << "\n";
                    oss.str("");
                    oss.clear();
                }
        }

        else if(str.find("password") == 0) {
            str = str.substr(9);
            std::istringstream iss(str);
            int i;
            iss >> i;

            oss << i << " - " << tpm->at(i).getLogin() << " : " << tpm->at(i).getPassword();
            std::cout << oss.str() << "\n";
            oss.str("");
            oss.clear();
        }
    } while(str.find("exit") == std::string::npos);      

    return 0;
}
#endif

